/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_invite.js                                 
*  Description  : 조인룸 초대자 리스트
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-15  김효순    Created. 
*  2012-06-15  김효순    Complete. 
*******************************************************************************/

jQuery(function($){
	function clsList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsList.prototype = {
		_init: function(){
			var $layer = $("tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").clone(true)
			}
			
			var $img1 = $layer.find("img[alt='수락']:first").clone(true);
			var $img2 = $layer.find("img[alt='수락대기']:first").clone(true);
			var $img3 = $layer.find("img[alt='초대거절']:first").clone(true);
			
			this._HTMLDom["$element"].find("img[alt='수락'], img[alt='수락대기'], img[alt='초대거절']").remove();
			
			this._HTMLDom["$element"].find("td:last").append($img1, $img2, $img3);
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, setMemberInfo: function(info){
			for(var i=0;i<this.listData.length;i++){
				if(this.listData[i]["아이디"] == info["아이디"]) break;
			}
			
			if(i<this.listData.length){
				this.listData[i]["닉네임"] = info["닉네임"];
				this.listData[i]["이미지"] = info["이미지"];
				this.listData[i]["성별"] = info["성별"];
				this.listData[i]["핸디"] = info["핸디"];
				this.listData[i]["평균타수"] = info["평균타수"];
				
				this.displayMember(i);
			}
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			var genderClass = {
				"남자": "sex_m"
				, "여자": "sex_f"
			}
			
			$layer.empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0) img").attr("src", _gf.getDefaultProfileImage()).width(35).height(35);
				$(this).find("td:eq(1)").html(thisClass.listData[n]["닉네임"]);
				$(this).find("td:eq(2) li:eq(0)").attr("class", genderClass[thisClass.listData[n]["성별"]]).html(thisClass.listData[n]["성별"]);
				$(this).find("td:eq(2) li:eq(1) span").html(thisClass.listData[n]["핸디"]);
				$(this).find("td:eq(2) li:eq(2) span").html(thisClass.listData[n]["평균타수"]);
				
				$(this).find("td:eq(3) img").hide().filter("[alt='" + thisClass.listData[n]["상태"] + "']").show();
			});
		}
		, displayMember: function(n){
			var $obj = this._HTMLDom["$layer"].find("tr:eq(" + n + ")");
			
			var genderClass = {
				"남성": "sex_m"
				, "여성": "sex_f"
			}
			
			$obj.find("td:eq(1)").html(this.listData[n]["닉네임"]);
			$obj.find("td:eq(0) img").attr("src", this.listData[n]["이미지"]).error(function(){
				$(this).attr("src", _gf.getDefaultProfileImage());
			});
			$obj.find("td:eq(2) li:eq(0)").attr("class", genderClass[this.listData[n]["성별"]]).html(this.listData[n]["성별"]);
			$obj.find("td:eq(2) li:eq(1) span").html(this.listData[n]["핸디"]);
			$obj.find("td:eq(2) li:eq(2) span").html(this.listData[n]["평균타수"]);
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent().next("div#tmpSpin").length == 0){
				$layer.parent().after("<div id='tmpSpin' style='position:relative; width:500px; height:200px;' />");
			}
			$layer.parent().next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().next("div#tmpSpin").hide().spin(false);
			$layer.show();
		}
	}
	
	function clsComm()
	{
		this.invitedList = null;
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, getInvitedList: function(arg){
			var thisClass = this;
			
//			arg = {
//				"조인룸키": null
//			}

			this.invitedList = [];
			
			//ajax
			var serviceName = "invitMemberList";
			document.objGlobal.ajax({
				"sid": "JR001"
				, "servicename": serviceName
			}, {
				"joinRoomSeq": arg["조인룸키"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						thisClass.invitedList.push({
							"아이디": obj["participantId"]
							/*, "이미지": "http://192.168.0.82:8080/sky72web/images/board/default_35_35.gif"
							, "닉네임": "닉네임" + i
							, "성별": (gender ? "남자": "여자")
							, "핸디": i
							, "평균타수": i*/
							, "상태": "수락대기"
						});
					}
				}
				
				document.objControl.finishInvitedList(res);
			});
		}
		, getInvitedData: function(){
			return this.invitedList;
		}
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objList = null;
		
		this._permitStart = null;
		
		this.joinroomKey = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objList = new clsList();
			
			this._bind();
			this._beforeStart();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.joinroomKey = _util.getParam("q_joinroomKey");
			
			if(!opener) self.close();
			if(!opener.document.objControl) self.close();
			
			//this.joinroomKey = opener.document.objControl.getJoinroomKey();
			
			if(!this.joinroomKey) self.close();
		}
		, _bind: function(){
			$("img[alt='확인'], img[alt='close']").click(function(){
				self.close();
			});
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _start: function(){
			this.objList.spinStart();
			this.objComm.getInvitedList({
				"조인룸키": this.joinroomKey
			});
		}
		, finishInvitedList: function(ret){
			var thisClass = this;
			
			this.objList.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisData = this.objComm.getInvitedData();
			
			for(var i=0;i<thisData.length;i++){
				_gf.getMemberInfo({
					"아이디": thisData[i]["아이디"]
					, "콜백": function(ret, info, args){
						thisClass.finishMemberInfo(ret, info, args);
					}
				});
			}
			
			this.objList.setList(thisData);
		}
		, finishMemberInfo: function(ret, info, args){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;

			this.objList.setMemberInfo(info);
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});